package com.atguigu.gmall.cart.service;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.cart.feign.GmallPmsClient;
import com.atguigu.gmall.cart.feign.GmallSmsClient;
import com.atguigu.gmall.cart.feign.GmallWmsClient;
import com.atguigu.gmall.cart.interceptors.LoginInterceptor;
import com.atguigu.gmall.cart.pojo.Cart;
import com.atguigu.gmall.common.bean.UserInfo;
import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.common.exception.CartException;
import com.atguigu.gmall.pms.entity.SkuAttrValueEntity;
import com.atguigu.gmall.pms.entity.SkuEntity;
import com.atguigu.gmall.sms.vo.ItemSaleVo;
import com.atguigu.gmall.wms.entity.WareSkuEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.concurrent.ListenableFuture;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class CartService {

    private static final String KEY_PREFIX = "cart:info:";
    private static final String PRICE_PREFIX = "cart:price:";

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private CartAsyncService asyncService;

    @Autowired
    private GmallPmsClient pmsClient;

    @Autowired
    private GmallSmsClient smsClient;

    @Autowired
    private GmallWmsClient wmsClient;

    /**
     * 新增购物车
     * @param cart：skuId count
     */
    public void addCart(Cart cart) {
        // 获取登录状态：userInfo
        String userId = getUserId();

        // 组装外层的key
        String key = KEY_PREFIX + userId;
        // this.redisTemplate.opsForHash();
        // 获取当前用户的内层购物车map
        BoundHashOperations<String, Object, Object> hashOps = this.redisTemplate.boundHashOps(key);
        // 判断当前用户是否包含该商品
        String skuId = cart.getSkuId().toString(); // 要新增的skuId
        BigDecimal count = cart.getCount(); // 要新增的数量
        if (hashOps.hasKey(skuId)){
            // 如果包含，更新数量(累加)
            String json = hashOps.get(skuId).toString();
            cart = JSON.parseObject(json, Cart.class);
            cart.setCount(cart.getCount().add(count)); // 购物车中已有数量 累加 新增的数量
            // 使用新的cart 覆盖掉 旧的cart
            // 同时更新mysql  同步 后续再去改成异步
            this.asyncService.updateCart(userId, skuId, cart);
        } else {
            // 如果不包含，新增一条记录
            cart.setUserId(userId);
            cart.setCheck(true);
            // 查询sku相关信息
            ResponseVo<SkuEntity> skuEntityResponseVo = this.pmsClient.querySkuById(cart.getSkuId());
            SkuEntity skuEntity = skuEntityResponseVo.getData();
            if (skuEntity == null){
                throw new CartException("要添加的商品不存在");
            }
            cart.setTitle(skuEntity.getTitle());
            cart.setPrice(skuEntity.getPrice());
            cart.setDefaultImage(skuEntity.getDefaultImage());

            // 查询库存
            ResponseVo<List<WareSkuEntity>> wareResponseVo = this.wmsClient.queryWareSkusBySkuId(cart.getSkuId());
            List<WareSkuEntity> wareSkuEntities = wareResponseVo.getData();
            if (!CollectionUtils.isEmpty(wareSkuEntities)){
                cart.setStore(wareSkuEntities.stream().anyMatch(wareSkuEntity -> wareSkuEntity.getStock() - wareSkuEntity.getStockLocked() > 0));
            }

            // 销售属性
            ResponseVo<List<SkuAttrValueEntity>> responseVo = this.pmsClient.querySaleAttrValuesBySkuId(cart.getSkuId());
            List<SkuAttrValueEntity> skuAttrValueEntities = responseVo.getData();
            cart.setSaleAttrs(JSON.toJSONString(skuAttrValueEntities));

            // 营销信息
            ResponseVo<List<ItemSaleVo>> salesResponseVo = this.smsClient.querySalesBySkuId(cart.getSkuId());
            List<ItemSaleVo> itemSaleVos = salesResponseVo.getData();
            cart.setSales(JSON.toJSONString(itemSaleVos));

            // 新增到数据库
            this.asyncService.insertCart(userId, cart);

            this.redisTemplate.opsForValue().set(PRICE_PREFIX + skuId, skuEntity.getPrice().toString());
        }
        hashOps.put(skuId, JSON.toJSONString(cart)); // redis
    }

    private String getUserId() {
        // 从拦截器中获取userInfo
        UserInfo userInfo = LoginInterceptor.getUserInfo();
        // 获取userKey
        String userId = userInfo.getUserKey();
        // 如果登录了（userId不为空），用userId覆盖掉userKey
        if (userInfo.getUserId() != null){
            userId = userInfo.getUserId().toString();
        }
        return userId;
    }

    /**
     * 新增成功后的回显查询
     * @param skuId
     * @return
     */
    public Cart queryCartBySkuId(Long skuId) {
        // 获取登录用户信息
        String userId = this.getUserId();

        // 获取当前用户的购物车操作对象
        BoundHashOperations<String, Object, Object> hashOps = this.redisTemplate.boundHashOps(KEY_PREFIX + userId);
        String json = hashOps.get(skuId.toString()).toString();
        if (StringUtils.isBlank(json)){
            throw new CartException("对应的购物车记录不存在");
        }
        return JSON.parseObject(json, Cart.class);
    }

    @Async
    public ListenableFuture<Void> execute1(){
        try {
            System.out.println("这是service中的execute1方法开始执行");
            TimeUnit.SECONDS.sleep(5);
            int i = 1/0;
            System.out.println("execute1方法执行完成============");
            return AsyncResult.forValue(null);
        } catch (InterruptedException e) {
            e.printStackTrace();
            return AsyncResult.forExecutionException(e);
        }
    }

    @Async
    public Future<String> execute2(){
        try {
            System.out.println("这是service中的execute2方法开始执行");
            TimeUnit.SECONDS.sleep(4);
            int i = 1/0;
            System.out.println("execute2方法执行完成============");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return AsyncResult.forValue("hello execute2");
    }

    public List<Cart> queryCarts() {
        UserInfo userInfo = LoginInterceptor.getUserInfo();
        // 1.查询未登录的购物车 相当于拿到内存的map<skuId, cartJson>
        BoundHashOperations<String, Object, Object> unLoginHashOps = this.redisTemplate.boundHashOps(KEY_PREFIX + userInfo.getUserKey());
        // 获取未登录用户的购物车json字符串集合
        List<Object> cartJsons = unLoginHashOps.values();
        // 如果未登录的购物车集合不为空，转化为购物车集合
        List<Cart> unLoginCarts = null;
        if (!CollectionUtils.isEmpty(cartJsons)){
            // 未登录的购物车集合
            unLoginCarts = cartJsons.stream().map(cartJson -> {
                Cart cart = JSON.parseObject(cartJson.toString(), Cart.class);
                // 查询实时价格缓存
                cart.setCurrentPrice(new BigDecimal(this.redisTemplate.opsForValue().get(PRICE_PREFIX + cart.getSkuId())));
                return cart;
            }).collect(Collectors.toList());
        }

        // 2.获取登录信息，判断是否登录（userId=null说明没有登录），没有登录则直接返回未登录的购物车即可
        Long userId = userInfo.getUserId();
        if (userId == null){
            return unLoginCarts;
        }

        // 3.如果登录了，需要合并未登录的购物车 到 已登录的购物车中
        BoundHashOperations<String, Object, Object> loginHashOps = this.redisTemplate.boundHashOps(KEY_PREFIX + userId);
        // 判断未登录的购物车是否为空，不为空则需要遍历未登录的购物车，然后合并到已登录的购物车中
        if (!CollectionUtils.isEmpty(unLoginCarts)){
            unLoginCarts.forEach(cart -> {
                // 参照新增购物车：如果已登录购物车中包含了该商品 则更新数量  如果没有包含该商品则新增一条记录
                String skuId = cart.getSkuId().toString();
                BigDecimal count = cart.getCount();
                if (loginHashOps.hasKey(skuId)){
                    String cartJson = loginHashOps.get(skuId).toString();
                    cart = JSON.parseObject(cartJson, Cart.class);
                    cart.setCount(cart.getCount().add(count));
                    // 更新到数据库
                    this.asyncService.updateCart(userId.toString(), skuId, cart);
                } else {
                    // 用userId 把userKey 给替换掉
                    cart.setUserId(userId.toString());
                    this.asyncService.insertCart(userId.toString(), cart);
                }
                loginHashOps.put(skuId, JSON.toJSONString(cart));
            });

            // 4.删除未登录的购物车
            this.redisTemplate.delete(KEY_PREFIX + userInfo.getUserKey());
            this.asyncService.deleteCartByUserId(userInfo.getUserKey());
        }

        // 5.查询已登录的购物车
        List<Object> loginCartJsons = loginHashOps.values();
        if (CollectionUtils.isEmpty(loginCartJsons)){
            return null;
        }
        return loginCartJsons.stream().map(cartJson -> {
            Cart cart = JSON.parseObject(cartJson.toString(), Cart.class);
            cart.setCurrentPrice(new BigDecimal(this.redisTemplate.opsForValue().get(PRICE_PREFIX + cart.getSkuId())));
            return cart;
        }).collect(Collectors.toList());
    }

    // Map<userId/userKey, Map<skuId, cartJson>>
    public void updateNum(Cart cart) {
        // 获取登录状态
        String userId = this.getUserId();

        // 根据登录信息查询当前用户的购物车
        BoundHashOperations<String, Object, Object> hashOps = this.redisTemplate.boundHashOps(KEY_PREFIX + userId);
        if (!hashOps.hasKey(cart.getSkuId().toString())){
            throw new CartException("当前用户没有该条购物车记录");
        }

        // 获取当前用户对应的购物车记录
        String cartJson = hashOps.get(cart.getSkuId().toString()).toString();
        BigDecimal count = cart.getCount();
        cart = JSON.parseObject(cartJson, Cart.class);
        cart.setCount(count);

        hashOps.put(cart.getSkuId().toString(), JSON.toJSONString(cart));
        this.asyncService.updateCart(userId, cart.getSkuId().toString(), cart);
    }

    public void deleteCart(Long skuId) {
        String userId = this.getUserId();

        BoundHashOperations<String, Object, Object> hashOps = this.redisTemplate.boundHashOps(KEY_PREFIX + userId);

        hashOps.delete(skuId.toString());
        this.asyncService.deleteCartByUserIdAndSkuId(userId, skuId);
    }

    public List<Cart> queryCheckedCartsByUserId(Long userId) {

        BoundHashOperations<String, Object, Object> hashOps = this.redisTemplate.boundHashOps(KEY_PREFIX + userId);
        List<Object> cartJsons = hashOps.values();
        if (CollectionUtils.isEmpty(cartJsons)){
            return null;
        }

        return cartJsons.stream().map(cartJson -> JSON.parseObject(cartJson.toString(), Cart.class)).filter(Cart::getCheck).collect(Collectors.toList());
    }
}
